--[[
    NuckPluck - An android game for learning the Thai alphabet
    Copyright (C) 2013 Nathan Kieffer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Contact: nathankieffer@gmail.com
--]]



local Vector = {}
Vector.__index = Vector
Vector.__unm = function(self)
   return Vector.new{x = -self.x, y = -self.y}
end


function Vector.new(o)
   local self = setmetatable({}, Vector)
   self.x = o.x
   self.y = o.y
   return self
end

function Vector:getMagnitude()
   return math.sqrt(math.pow(self.x, 2) + math.pow(self.y, 2))
end

function Vector:normalize()
   local mag = self:getMagnitude()
   self.x = mag ~= 0 and self.x / mag or 0
   self.y = mag ~= 0 and self.y / mag or 0
end

function Vector.fromPoints(pt1, pt2)
   local v = Vector.new{ x=pt2.x - pt1.x, y=pt2.y - pt1.y}
   return v
end

function Vector.fromAngle(angle)
   local radians = angle * math.pi / 180
   local v = Vector.new{x=math.cos(radians), y = math.sin(radians)}
   return v
end

function Vector:toAngle()

   local angle = math.atan2(self.x, self.y)
   return ((-angle/ math.pi * 180)) % 360

end

return Vector